package www.superbytecode.com.controller;

import javafx.fxml.FXML;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import www.superbytecode.com.iframe.UIAlert;
import www.superbytecode.com.model.DatabaseAttribute;
import www.superbytecode.com.util.ConfigHelper;
import www.superbytecode.com.util.DbUtil;


import java.net.URL;
import java.util.ResourceBundle;

/**
 * All rights Reserved, Designed By www.super-bytecode.com
 *
 * @ProjectName superbytecode-builder
 * @Package www.superbytecode.com.controller
 * @ClassName ConnectionController
 * @Description 数据库连接功能入口
 * @Author [Lance Ting]
 * @Date 2020/7/14 20:37
 * @Version V1.0
 * @Copyright: 2020 www.super-bytecode.com Inc. All rights reserved.
 * @TODO：本文件Lance Ting所作, 如果转载或使用请标明具体出处!
 **/
public class ConnectionController extends BaseFXController {
    private static final Logger logger = LoggerFactory.getLogger(ConnectionController.class);
    /**
     *
     */
    @FXML
    private TextField nameField;
    /**
     *
     */
    @FXML
    private TextField hostField;
    /**
     *
     */
    @FXML
    private TextField portField;
    /**
     *
     */
    @FXML
    private TextField userNameField;
    /**
     *
     */
    @FXML
    private TextField passwordField;
    /**
     *
     */
    @FXML
    private TextField schemaField;
    /**
     *
     */
    @FXML
    private ChoiceBox<String> encodingChoice;
    /**
     *
     */
    @FXML
    private ChoiceBox<String> dbTypeChoice;
    /**
     *
     */
    private MainUIController mainUIController;


    @Override
    public void initialize(URL location, ResourceBundle resources) {
    }

    @FXML
    void saveConnection() {
        DatabaseAttribute config = extractConfigForUI();
        if (config == null) {
            return;
        }
        try {
            ConfigHelper.saveDatabaseConfig(config.getName(), config);
            getDialogStage().close();
            mainUIController.loadLeftDBTree();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            UIAlert.showErrorAlert(e.getMessage());
        }
    }

    @FXML
    void testConnection() {
        DatabaseAttribute config = extractConfigForUI();
        if (config == null) {
            return;
        }
        try {
            String url = DbUtil.getConnectionUrlWithSchema(config);
            System.out.println(url);
            DbUtil.getConnection(config);
            UIAlert.showInfoAlert("连接成功");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            UIAlert.showWarnAlert("连接失败");
        }
    }

    @FXML
    void cancel() {
        getDialogStage().close();
    }

    void setMainUIController(MainUIController controller) {
        this.mainUIController = controller;
    }

    private DatabaseAttribute extractConfigForUI() {
        String name = nameField.getText();
        String host = hostField.getText();
        String port = portField.getText();
        String userName = userNameField.getText();
        String password = passwordField.getText();
        if ("".equals(password)) {
            password = "";
        }
        String encoding = encodingChoice.getValue();
        String dbType = dbTypeChoice.getValue();
        String schema = schemaField.getText();
        DatabaseAttribute config = new DatabaseAttribute();
        config.setName(name);
        config.setDbType(dbType);
        config.setHost(host);
        config.setPort(port);
        config.setUsername(userName);
        config.setPassword(password);
        config.setSchema(schema);
        config.setEncoding(encoding);
        if (StringUtils.isAnyEmpty(name, host, port, userName, encoding, dbType, schema)) {
            UIAlert.showWarnAlert("所有字段都必填");
            return null;
        }
        return config;
    }

}